﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using CustomerContactSystem.Core.Model;

namespace CustomerContactSystem.WebForms.Admin.Users
{
    public partial class Create : BasePage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!IsValidPageRequest(true, true, "")) {
                return;
            }

            GetDepartments();
            GetGroups();
            GetRoles();
        }

        private void GetDepartments()
        {
            var departments = DepartmentRepository.QueryOver().OrderBy(d => d.DepartmentName).Asc.List();
            BindDropDownList(DepartmentList, departments, "DepartmentName", "Id", "--Select--", "0");
        }

        private void GetGroups() {
            var groups = UserGroupRepository.QueryOver().OrderBy(u => u.GroupName).Asc.List();
            BindDropDownList(GroupList, groups, "GroupName", "Id", "--Select--", "0");
        }

        private void GetRoles() {
            var roles = UserRoleRepository.QueryOver().OrderBy(r => r.RoleName).Asc.List();
            RoleList.DataSource = roles;
            RoleList.DataBind();
        }

        protected void CreateNewUserClick(object sender, EventArgs e) {
            var department = DepartmentRepository.LoadReference(int.Parse(DepartmentList.SelectedItem.Value));
            var userGroup = UserGroupRepository.LoadReference(int.Parse(GroupList.SelectedItem.Value));
            var user = new User
                {
                    Department = department,
                    UserGroup = userGroup,
                    DisplayName = DisplayName.Text,
                    Email = Email.Text,
                    Username = Username.Text,
                    Password = Password.Text,
                    PasswordMatch = Password2.Text,
                    FirstPointOfContact = FrontLine.Checked,
                    AreaCovered = AreaCovered.Text,
                    IsActive = true
                };

            if (!IsValidObject(user, ErrorMsg)) {
                return;
            }

            UserRepository.Add(user);

            foreach (RepeaterItem ri in RoleList.Items) {
                if (ri.ItemType != ListItemType.Item && ri.ItemType != ListItemType.AlternatingItem) {
                    continue;
                }
                var chk = (CheckBox) ri.FindControl("RoleChk");
                if (!chk.Checked) {
                    continue;
                }
                var role = UserRoleRepository.QueryOver().Where(r => r.RoleName == chk.Text).SingleOrDefault();
                if (role != null) {
                    user.AddRole(role);
                }
            }

            if (!user.Roles.Any()) {
                var defaultRoleId = ConfigSettings.DefaultRoleId;
                var defaultRole = UserRoleRepository.LoadReference(defaultRoleId);
                user.AddRole(defaultRole);
            }

            UserRepository.Update(user);
            Response.Redirect("~/Admin/Users/List.aspx#" + user.Id);
        }
    }
}